package com.cl.base.auth.handler;

import cn.hutool.core.util.StrUtil;
import com.cl.base.constant.ResultCode;
import com.cl.base.util.JsonUtil;
import com.cl.base.util.ResultVo;
import com.cl.base.util.ServletUtils;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.Serializable;

/**
 * 认证失败处理类 返回未授权
 */
@Slf4j
@Component
@RequiredArgsConstructor
public class ClAuthenticationEntryPoint implements AuthenticationEntryPoint, Serializable {
    private final JsonUtil jsonUtil;

    @Override
    public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException authException) throws IOException, ServletException {
        ServletUtils.renderString(response, jsonUtil.toString(ResultVo.resultVo(ResultCode.AUTHENTICATION_ERROR)));
    }
}
